﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DALLayer.Providers;
using DataDefinition;
using Objects;

namespace BusinessLogicLayer.Manager
{
    public class ArticleTypeManager:ManagerBase<ArticleTypeDataProvider>
    {
       #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="ArticleTypeManager" /> class.
        /// </summary>
        public ArticleTypeManager() { } 
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new ArticleTypeDataProvider();
        }
        #endregion

        #region [Public Methods]
        /// <summary>
        /// Gets all the article types
        /// </summary>
        /// <returns>article type list</returns>
        public List<ArticleType> GetArticleTypes()
        {
            //The article type details
            List<ArticleType> articleDetails = null;
            try
            {
                //Get the article type details list
                articleDetails = this.GetArticleTypes(0, false);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The article type details list to return
            return articleDetails;
        }
        /// <summary>
        /// Gets all the article type by article id
        /// </summary>
        /// <param name="articleId">articleId</param>
        /// <returns>article type list</returns>
        public List<GetArticleTypeByArticleID> GetArticleTypeByArticleID(int articleId)
        {
            //The article type details
            List<GetArticleTypeByArticleID> articleTypeDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = this.DataProvider.GetArticleTypeByArticleID(articleId);
                if (dataSet != null && dataSet.GetArticleTypeArticleID != null && dataSet.GetArticleTypeArticleID.Rows != null)
                {
                    //Set the returned filled list
                    articleTypeDetails = FillGetArticleTypeArticleIDObjectsByEntities(dataSet.GetArticleTypeArticleID);
                }
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The article type details list to return
            return articleTypeDetails;
        }

     
        #endregion

        #region [Private Methods]
        /// <summary>
        /// The method that gets the list of article types or just one article type
        /// </summary>
        /// <param name="articleTypeId">The article type id</param>
        /// <param name="selectById">The check to see if select by id or not</param>
        /// <returns>article type list</returns>
        private List<ArticleType> GetArticleTypes(int articleTypeId, bool selectById)
        {
            //The article type details to be returned
            List<ArticleType> articleTypeDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetAllArticleTypes(articleTypeId, selectById);

                if (dataSet != null && dataSet.ArticleType != null && dataSet.ArticleType.Rows != null)
                {
                    //Set the returned filled list
                    articleTypeDetails = FillArticleTypeObjectsByEntities(dataSet.ArticleType);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the article type details list
            return articleTypeDetails;

        }
        /// <summary>
        /// Fills the article type object
        /// </summary>
        /// <param name="dataTable">the data table</param>
        /// <returns>The article type list</returns>
        private List<ArticleType> FillArticleTypeObjectsByEntities(AutoFixDataSet.ArticleTypeDataTable dataTable)
        {
            //The article list that contains the objects
            //Loop through the data table and create new ArticleTypeObject 
            //finally select the ArticleTypeObject and convert to list
            List<ArticleType> articleTypeDetails = (from ato in
                                                (from at in dataTable.AsEnumerable<AutoFixDataSet.ArticleTypeRow>()
                                                 select new
                                                 {
                                                     ArticleTypeObject = new ArticleType()
                                                     {
                                                         ArticleTypeID = at.ArticleTypeID,
                                                         ArticleTypeName = at.ArticleTypeName,
                                                         IsActive = at.IsActive
                                                     }
                                                 })
                                            select ato.ArticleTypeObject).ToList();
            //Return the filled article type details list
            return articleTypeDetails;
        }
        /// <summary>
        /// Fills the article type object
        /// </summary>
        /// <param name="dataTable">the data table</param>
        /// <returns>The article type list</returns>
        private List<GetArticleTypeByArticleID> FillGetArticleTypeArticleIDObjectsByEntities(AutoFixDataSet.GetArticleTypeArticleIDDataTable dataTable)
        {
            //The get article type by articleID list that contains the objects
            //Loop through the data table and create new ArticleTypeObject 
            //finally select the ArticleTypeObject and convert to list
            List<GetArticleTypeByArticleID> articleTypeDetails = (from ato in
                                                                      (from at in dataTable.AsEnumerable<AutoFixDataSet.GetArticleTypeArticleIDRow>()
                                                         select new
                                                         {
                                                             ArticleTypeObject = new GetArticleTypeByArticleID()
                                                             {
                                                                 ArticleTypeID = at.ArticleTypeID
                                                             }
                                                         })
                                                    select ato.ArticleTypeObject).ToList();
            //Return the filled article type details list
            return articleTypeDetails;
        }
        #endregion
    }
}
